/**
 *
 */
package org.gbif.crawler.console.action;

import org.gbif.crawler.service.api.model.CrawlableResource;
import org.gbif.crawler.service.api.service.CrawlableResourceService;
import org.gbif.utils.HttpUtil;

import com.google.inject.Inject;
import com.google.inject.name.Named;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author tim
 */
public class CrawlableResourceAction extends ActionSupport {

  /**
   * Generated serial version UID
   */
  private static final long serialVersionUID = 7174996226611194088L;
  private static final Logger LOG = LoggerFactory.getLogger(CrawlableResourceAction.class);
  private CrawlableResource crawlableResource;
  private String key;
  private static final String SUCCESS_204 = "204";
  private static final String SUCCESS_FORM_SAVE = "redirect";
  protected static final String RESOURCE_IDENTIFIERS_FILE = "meta.json";
  // for redirect action
  private String redirectTarget;
//  @Inject
//  @Named("crawl.crawlableResource.publisher")
//  private Publisher crawlResourcePublisher;
  @Inject
  private CrawlableResourceService crawlableResourceService;
  @Inject
  @Named("baseDirectory")
  private String baseDirectory;
  @Inject
  @Named("registryResourceIdentifierUrl")
  private String registryResourceIdentifierUrl;
  private HttpUtil util = new HttpUtil();

  public String attempt() {
    detail();
    return ActionSupport.SUCCESS;
  }

  public String crawl() {
    if (StringUtils.isNotBlank(key)) {
        //crawlResourcePublisher.publish(new CrawlResource(crawlableResource.getKey()));
    }
    return SUCCESS_204;
  }

  public String detail() {
    if (StringUtils.isNotBlank(key)) {
      crawlableResource = crawlableResourceService.get(key);
    }
    if (crawlableResource != null) {
      return ActionSupport.SUCCESS;
    } else {
      return ActionSupport.ERROR;
    }
  }

  public CrawlableResource getCrawlableResource() {
    return crawlableResource;
  }

  public String getKey() {
    return key;
  }

  public String getRedirectTarget() {
    return redirectTarget;
  }

  public String save() {
    CrawlableResource currentSaved = crawlableResourceService.get(crawlableResource.getKey());
    currentSaved.setNextFireTime(crawlableResource.getNextFireTime());
    currentSaved.setCrawlPeriodDays(crawlableResource.getCrawlPeriodDays());
    currentSaved.setHideFromUnderCrawledList(crawlableResource.isHideFromUnderCrawledList());
    currentSaved.setHideFromOverCrawledList(crawlableResource.isHideFromOverCrawledList());
    currentSaved.setHideFromOfflineCrawledList(crawlableResource.isHideFromOfflineCrawledList());
    currentSaved.setAdminPriority(crawlableResource.getAdminPriority());
    crawlableResourceService.update(currentSaved);
    // set up the redirect to go back to the form
    redirectTarget = "CrawlableResource/detail?key=" + currentSaved.getKey();
    return SUCCESS_FORM_SAVE;
  }

  public void setCrawlableResource(CrawlableResource crawlableResource) {
    this.crawlableResource = crawlableResource;
  }

  public void setKey(String key) {
    this.key = key;
  }

  public void setRedirectTarget(String redirectTarget) {
    this.redirectTarget = redirectTarget;
  }
}
